package com.example.proxy;

import com.example.proxy.service.Calculate;
import com.example.proxy.service.impl.CalculateImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class Demo01 {
    private static Calculate calculate=new CalculateImpl();
    public static void main(String[] args) {
         Calculate obj = (Calculate)Proxy.newProxyInstance(
                calculate.getClass().getClassLoader(),
                calculate.getClass().getInterfaces(),
                new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("执行前代理");
                Object invoke = method.invoke(calculate, args);
                System.out.println("后置增强");
                return invoke;
            }
        });
        int cal = obj.cal2(2);
        System.out.println(cal);
    }
}
